//
//  QChangeNickNameVC.m
//  QoilPublic
//
//  Created by zhuang on 16/7/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QChangeNickNameVC.h"

@interface QChangeNickNameVC ()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;



@end

@implementation QChangeNickNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改昵称";
    self.view.backgroundColor = [QColorUtility defaultBackGroundColor];
    [self setupForDismissKeyboard];
    _nameTextField.delegate = self;
    [_agreeBtn blueStyle];
    self.nameTextField.text = self.nickNameStr;
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(changeBack)];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}


- (void)changeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)agreeButtonAction:(id)sender {
    
    [self requestData];
}

- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"myNickname"];
    NSDictionary *param = [QParamService changeNickName:self.nameTextField.text];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"修改成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if(self.backBlock){
                [QUserInfo sharedInfo].loginModel.nickName = _nameTextField.text;
                [QDataUtility updateLoginModel:[QUserInfo sharedInfo].loginModel];
                self.backBlock();
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSignup object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}


@end
